package game;

import org.springframework.jdbc.core.JdbcTemplate;
import utils.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class GamePanel extends JPanel {

    GameManager game = GameManager.getInstance();

    public GamePanel() {
        super.setFocusable(true);//激活窗口焦点
        super.addKeyListener(new KeyAdapter() {
            //addKeyListener键盘监听
            //KeyAdapter 只包含 按下和释放按键 这两个方法
            @Override
            public void keyPressed(KeyEvent e) {
                int code = e.getKeyCode();
                if (code == KeyEvent.VK_SPACE && GameData.gameState == 1) {
                    GameData.gameState = 2;
                    game.trex.getState(2);
                }
                if (code == KeyEvent.VK_SPACE && GameData.gameState == 3) {
                    game.gameInit();
                }
                game.trex.jumpstate(code);
                game.trex.bowdown(code, 1);
                game.trex.disappear(code);
            }//按下

            @Override
            public void keyReleased(KeyEvent e) {
                int code = e.getKeyCode();
                game.trex.bowdown(code, 2);
            }//释放
        });
    }

    SkinManager skinManager = new SkinManager();
    Font font16 = new Font(null, Font.BOLD, 16);
    Font font12 = new Font(null, Font.PLAIN, 12);

    //默认字体 字体样式，字体大小
    //字体颜色
    public void paint(Graphics g) {
        if (GameData.gameState == 1) {
            g.setFont(font16);
            g.setColor(GameData.color);
            g.drawString("空格键 开始", 250, 50);
            g.drawString("下键冲刺  x键可能有惊喜？", 270, 70);
            game.background.draw(g);
            game.trex.draw(g);
        }

        if (GameData.gameState == 2) {
            if (game.trex.skill) {
                g.setFont(font12);
                g.setColor(GameData.color);
                g.drawString("取消碰撞", game.trex.x + 6, game.trex.y - 20);

            }
            game.background.draw(g);
            game.background.move();

            for (Catus catus :
                    game.catusList) {
                catus.draw(g);
                catus.move();
                catus.drawBounds(g);
                if (game.trex.impackCheck(catus)) {
                    game.gameOver();
                }
            }
            GameData.catusInterval += 10;

            for (int i = 0; i < game.cloudList.size(); i++) {
                Cloud cloud = game.cloudList.get(i);
                cloud.draw(g);
                cloud.move();
            }
            GameData.cloudInterval += 20 + RandomUtils.randomInt(0, 3);

            game.pterosaur.draw(g);
            game.pterosaur.move();
            game.pterosaur.drawBounds(g);
            if (game.trex.impackCheck(game.pterosaur)) {
                game.gameOver();
            }

            game.trex.draw(g);
            game.trex.move();
            game.trex.drawBounds(g);
            game.scoreBoard.addScore();
        }
        if (GameData.gameState == 3) {
            g.drawImage(GameData.gameoverImg, 200, 40, GameData.gameoverImg.getWidth(), GameData.gameoverImg.getHeight(), null);
            g.setFont(font16);
            g.setColor(GameData.color);
            g.drawString("空格键 重新开始", 230, 80);
            try {
                JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtil.getDataSource());
                String sql = "update user set MaxScore = ? where id = ?";
                String sql1 = "update score set Score = ?, All_jump = ?, jump_catus = ?, avoid_pterosaur = ? WHERE id = ?";
                jdbcTemplate.update(sql, GameData.highest_score, GameData.id);
                jdbcTemplate.update(sql1, GameData.highest_score,GameData.All_jump,GameData.jump_catus,GameData.avoid_pterosaur,GameData.id);
            } catch (Exception e) {
                e.printStackTrace();
            }
            GameData.current_score = 0;
        }
        game.scoreBoard.drawScore(g, GameData.highest_score, GameData.current_score);
        skinManager.draw(g);
    }
}
